package com.ruoyi.system.service.Evaluating.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Evaluating.EvaluationResult;
import com.ruoyi.system.domain.Evaluating.IndicatorEvaluationScore;
import com.ruoyi.system.domain.dto.EvaluationResultSearchDto;
import com.ruoyi.system.mapper.Evaluating.EvaluationResultMapper;
import com.ruoyi.system.service.Evaluating.IEvaluationResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-04
 */
@Service
public class EvaluationResultServiceImpl implements IEvaluationResultService
{
    @Autowired
    private EvaluationResultMapper evaluationResultMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public EvaluationResult selectEvaluationResultById(Long id)
    {
        return evaluationResultMapper.selectEvaluationResultById(id);
    }

    @Override
    public int getTotalResultsCount()
    {
        return evaluationResultMapper.selectTotalResults();
    }

    //todo:计算得分
    // public Float evaluateSelectedStandard(Indicator indicator, Standard standard, ModelMethod modelmethod)
    // {
    //     return 12.2f;
    // }

    //todo:计算细分
    // public List<Float> evaluateSelectedStandardDetaild(Indicator indicator,Standard standard,ModelMethod modelmethod)
    // {
    //
    // }


    @Override
    public List<IndicatorEvaluationScore> selectIndicatorEvaluationScoreByResultId(String evaluationResultId) {
        Long LongId = null;
        if (StringUtils.isNotBlank(evaluationResultId)) {
            LongId = Long.parseLong(evaluationResultId);
        }
        return evaluationResultMapper.selectIndicatorEvaluationScoreResultById(LongId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param evaluationResult 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<EvaluationResult> selectEvaluationResultList(EvaluationResult evaluationResult)
    {
        return evaluationResultMapper.selectEvaluationResultList(evaluationResult);
    }

    @Override
    public List<EvaluationResult> searchEvaluationResultList(EvaluationResultSearchDto evaluationResultSearchDto) {
        Long LongId = null;
        Integer intstatus = null;
        String id = evaluationResultSearchDto.getId();
        if ("null".equals(id)) {
            id = null;
        }

        String status = evaluationResultSearchDto.getStatus();
        if ("null".equals(status)) {
            status = null;
        }

        String startDate = evaluationResultSearchDto.getStartDate();
        if ("null".equals(startDate)) {
            startDate = null;
        }

        String endDate = evaluationResultSearchDto.getEndDate();
        if ("null".equals(endDate)) {
            endDate = null;
        }

        String standardName = evaluationResultSearchDto.getStandardName();
        if ("null".equals(standardName)) {
            standardName = null;
        }

        String projectName = evaluationResultSearchDto.getProjectName();
        if ("null".equals(projectName)) {
            projectName = null;
        }
        String evalulationName = evaluationResultSearchDto.getEvaluationName();
        if ("null".equals(evalulationName)) {
            evalulationName = null;
        }

        String standardCode=evaluationResultSearchDto.getStandardCode();

        if(id!=null) {
            if (StringUtils.isNotBlank(id)) {
                LongId = Long.parseLong(id);
            }
        }
        if(status!=null) {
            if (StringUtils.isNotBlank(status) || status.equals("0")) {
                intstatus = Integer.parseInt(status);
            }
        }
        Date start_date = new Date();
        Date end_date = new Date();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        List<EvaluationResult> evaluationResults = null;
        if (startDate != null && !startDate.trim().isEmpty()) {
            LocalDate localDate = LocalDate.parse(startDate, formatter);
            start_date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        }

        if (endDate != null && !endDate.trim().isEmpty()) {
            LocalDate localDate = LocalDate.parse(endDate, formatter);
            end_date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        }

            EvaluationResult evaluationResult = new EvaluationResult();
            evaluationResult.setId(LongId);
            evaluationResult.setpublicStatus(intstatus);
            evaluationResult.setstandardName(standardName);
            evaluationResult.setProjectName(projectName);
            evaluationResult.setEvaluationName(evalulationName);
            evaluationResult.setStandardCode(standardCode);
        evaluationResults = evaluationResultMapper.selectEvaluationResultList(evaluationResult);
        Iterator<EvaluationResult> iterator = evaluationResults.iterator();
        if(startDate!=null&&endDate!=null) {
            if (!startDate.isEmpty() || !endDate.isEmpty()) {
                while (iterator.hasNext()) {
                    EvaluationResult result = iterator.next();
                    Date evaluateDate = result.getEvaluateDate();
                    if ((evaluateDate.before(start_date)) || (evaluateDate.after(end_date))) {
                        iterator.remove();
                    }
                }
            }
        }
        return evaluationResults;

    }

    @Override
    public List<EvaluationResult> selectEvaluationResultTrend(String standardCode) {
        EvaluationResult evaluationResult=new EvaluationResult();
        //Long LongId = null;
        //if(standardId!=null) {
        //    if (StringUtils.isNotBlank(standardId)) {
        //        LongId = Long.parseLong(standardId);
        //    }
        //}
        evaluationResult.setStandardCode(standardCode);
        List<EvaluationResult> evaluationResults=evaluationResultMapper.selectEvaluationResultList(evaluationResult);

        if (evaluationResults != null) {
            // Sort the results by date in ascending order.
            // Assuming getDate() returns a Date or LocalDate object.
            evaluationResults.sort(Comparator.comparing(EvaluationResult::getEvaluateDate));
        }
        return evaluationResults;
    }
    @Override
    public List<EvaluationResult> selectEvaluationResultWithPublic(Integer _public)
    {
        EvaluationResult evaluationResult = new EvaluationResult();
        if(_public!=0&&_public!=1)
        {
            throw new RuntimeException("public status error occur");
        }
        evaluationResult.setpublicStatus(_public);
        List<EvaluationResult> res=evaluationResultMapper.selectEvaluationResultList(evaluationResult);
        if (res != null) {
            // Sort the results by date in ascending order.
            // Assuming getDate() returns a Date or LocalDate object.
            res.sort(Comparator.comparing(EvaluationResult::getEvaluateDate));
        }
        return res;
    }
    /**
     * 新增【请填写功能名称】
     *
     * @param evaluationResult 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertEvaluationResult(EvaluationResult evaluationResult)
    {
        evaluationResult.setCreateTime(DateUtils.getNowDate());
        return evaluationResultMapper.insertEvaluationResult(evaluationResult);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param evaluationResult 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateEvaluationResult(EvaluationResult evaluationResult)
    {
        return evaluationResultMapper.updateEvaluationResult(evaluationResult);
    }

    public int updateEvaluationResultbyName(String EvaluationName,Long id)
    {
        EvaluationResult tmp=selectEvaluationResultById(id);
        tmp.setEvaluationName(EvaluationName);

        return this.updateEvaluationResult(tmp);
    }

    @Override
    public int alterEvaluationResultIsPublic(Long EvaluationResultId,Integer publicStatus) {
        EvaluationResult tmp=selectEvaluationResultById(EvaluationResultId);
        tmp.setpublicStatus(publicStatus);
        System.out.println("修改"+tmp);
        return this.updateEvaluationResult(tmp);
    }

    @Override
    public int batchInsert(List<EvaluationResult> evaluationResults) {
        return evaluationResultMapper.batchInsert(evaluationResults);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    //@Override
    //public int deleteEvaluationResultByIds(Long[] ids)
    //{
        //return evaluationResultMapper.deleteEvaluationResultByIds(ids);
    //}

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    //@Override
    //public int deleteEvaluationResultById(Long id)
    //{
    //    return evaluationResultMapper.deleteEvaluationResultById(id);
    //}
}
